package com.warm.orm.interceptor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.easy.query.core.basic.extension.interceptor.EntityInterceptor;
import com.easy.query.core.expression.sql.builder.EntityInsertExpressionBuilder;
import com.easy.query.core.expression.sql.builder.EntityUpdateExpressionBuilder;
import com.warm.auth.helper.LoginHelper;
import com.warm.core.constant.HttpStatus;
import com.warm.core.exception.ServiceException;
import com.warm.orm.domain.BaseEntity;

import java.util.Date;

/**
 * 对象拦截器
 *
 * @author 胡高
 */
public class EqEntityInterceptor implements EntityInterceptor {

    /* (non-Javadoc)
     * @see com.easy.query.core.basic.extension.interceptor.Interceptor#apply(java.lang.Class)
     */
    @Override
    public boolean apply(Class<?> entityClass) {
        return ClassUtil.isAssignable(BaseEntity.class, entityClass);
    }

    /* (non-Javadoc)
     * @see com.easy.query.core.basic.extension.interceptor.EntityInterceptor#configureInsert(java.lang.Class, com.easy.query.core.expression.sql.builder.EntityInsertExpressionBuilder, java.lang.Object)
     */
    @Override
    public void configureInsert(Class<?> entityClass, EntityInsertExpressionBuilder entityInsertExpressionBuilder,
                                Object object) {
        try {
            if (ObjectUtil.isNotNull(object)) {
                BaseEntity entity = (BaseEntity) object;
                Date date = DateUtil.date();
                String username = StrUtil.isNotBlank(entity.getCreateBy())
                        ? entity.getCreateBy() : LoginHelper.getUsernameNe();
                entity.setCreateTime(date);
                entity.setUpdateTime(date);
                entity.setCreateBy(username);
                entity.setUpdateBy(username);
            }
        } catch (Exception e) {
            throw new ServiceException("新增自动注入异常 => " + e.getMessage(), HttpStatus.UNAUTHORIZED);
        }
    }

    /* (non-Javadoc)
     * @see com.easy.query.core.basic.extension.interceptor.EntityInterceptor#configureUpdate(java.lang.Class, com.easy.query.core.expression.sql.builder.EntityUpdateExpressionBuilder, java.lang.Object)
     */
    @Override
    public void configureUpdate(Class<?> entityClass, EntityUpdateExpressionBuilder entityUpdateExpressionBuilder,
                                Object object) {

        try {
            if (ObjectUtil.isNotNull(object)) {
                BaseEntity entity = (BaseEntity) object;
                entity.setUpdateBy(LoginHelper.getUsernameNe());
                entity.setUpdateTime(DateUtil.date());
            }
        } catch (Exception e) {
            throw new ServiceException("修改自动注入异常 => " + e.getMessage(), HttpStatus.UNAUTHORIZED);
        }
    }

    /* (non-Javadoc)
     * @see com.easy.query.core.basic.extension.interceptor.Interceptor#name()
     */
    @Override
    public String name() {
        return ClassUtil.getClassName(EqEntityInterceptor.class, true);
    }

}
